import { PaginationQueryDto } from "src/common/dto/paginationQuery.dto";
import { pickBy, mapValues } from 'lodash'
import { In } from "typeorm";

/**
 * 获取分页数据
 */
export const getPaginationData = <T extends PaginationQueryDto>({ current, pageSize, ...where }: T) => {
    const skip = (current - 1) * pageSize;
    const take = pageSize;
    return { skip, take, where };
};

/**
 * 获取筛选条件对象
 */
export const getWhere = (where: any) => {
    // 过滤
    where = pickBy(where, (v) => ![undefined, null, ''].includes(v));
    // 转化
    where = mapValues(where, (v) => {
        if (!isNaN(Number(v))) {
            v += '';
        } else if (Array.isArray(v)) {
            v = v.length ? In(v) : undefined;
        };
        return v;
    });
    return where;
};